<template>
  <el-row :gutter="20">
    <el-col :span="6"><category @tree-node-click="treenodeclick"></category></el-col>
    <el-col :span="18">
      <div class="mod-config">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList">
          <el-form-item>
            <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button type="success" @click="getAllDataList()">查询全部</el-button>
            <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button type="danger" @click="deleteHandle()">删除</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="dataList" style="width: 100%" border @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="attrId" label="id" header-align="center" align="center"></el-table-column>
          <el-table-column prop="attrName" label="属性名称" header-align="center" align="center"></el-table-column>
          <el-table-column prop="searchType" label="可检索" header-align="center" align="center" v-if="attrtype == 1">
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.searchType===1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column prop="valueType" label="属性类型" header-align="center" align="center">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.valueType === 0">单选</el-tag>
              <el-tag v-else>多选</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="icon" label="属性图标" header-align="center" align="center"></el-table-column>
          <el-table-column prop="valueSelect" label="可选值" header-align="center" align="center">
            <template slot-scope="scope">
              <el-tooltip placement="top">
                <div slot="content">
                  <span v-for="(i,index) in scope.row.valueSelect.split(';').join()" :key="index">{{i}}</span>
                </div>
                <el-tag>{{scope.row.valueSelect.split(";")[0]+" ..."}}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="enable" label="启用" header-align="center" align="center">
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.enable===1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column prop="catelogName" header-align="center" align="center" label="所属分类"></el-table-column>
          <el-table-column
            v-if="attrtype === 1"
            prop="attrGroupName"
            header-align="center"
            align="center"
            label="所属分组"
          ></el-table-column>
          <el-table-column prop="showDesc" v-if="attrtype===1" label="快速展示" header-align="center" align="center">
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.showDesc===1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" header-align="center" align="center" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.attrId)">修改</el-button>
              <el-button type="text" @click="deleteHandle(scope.row.attrId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <add-or-update ref="addOrUpdate" v-if="addOrUpdateVisible" @refreshDataList="getDataList" :type="attrtype"></add-or-update>
      </div>
    </el-col>
  </el-row>

</template>

<script>
import Category from '../common/category.vue'
import AddOrUpdate from './attr-add-or-update.vue'
import attrService from '@/api/product/attr.js'
export default {
  name: "",
  components: {Category,AddOrUpdate},
  props: {
    attrtype: {
      type: Number,
      default: 1
    }
  },
  data(){
    return {
      selectionDataList: [],
      pageSize: 10,
      totalPage: 0,
      pageIndex: 1,
      dataForm: {
        key: ''
      },
      addOrUpdateVisible: false,
      dataList: []
    }
  },
  methods: {
    treenodeclick(data, node, component) {
      console.log('tree node 被点击', data, node, component)
      if( node.level === 3) {
        this.catId = data.catId ? data.catId : -1
        this.getDataList()
      }
    },
    getDataList(){
      console.log(`getDataList被调用,key=${this.dataForm.key}`)
      let data = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key
      }
      attrService.getDataList(data, this.attrtype, this.catId ? this.catId : 0).then(({data})=>{
        console.log(data)
        if( data && data.code === 0) {
          this.dataList = data.page.list
        }
      })
    },
    getAllDataList() {
      console.log('getAllDataList被调用')
      this.catId = 0
      this.getDataList()
    },
    addOrUpdateHandle(attrId) {
      console.log('addOrUpdateHandle被调用，attrId为', attrId)
      this.addOrUpdateVisible = true;
      this.$nextTick(()=> {
        this.$refs.addOrUpdate.init(attrId)
      })
    },
    deleteHandle(attrId) {
      console.log('deleteHandle被调用，attrId为', attrId)
      let ids = attrId ? [attrId] : this.selectionDataList.map(attr => attr.attrId)
      if( ids.length === 0) { return }
      console.log('ids为', ids)
      attrService.remove(ids).then(({data})=>{
        if ( data && data.code === 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getDataList()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    // 当前页
    handleCurrentChange( val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 每页数
    handleSizeChange( val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    selectionChangeHandle(val) {
      console.log('选择',val)
      this.selectionDataList = val
    }

  },
  // 创建完成,可以访问this对象
  created() {
    this.getAllDataList()
  },
  // 挂载完成，可以访问dom元素
  mounted() {
  },

  watch: {},
  calculate(){},
  // 创建之前
  beforeCreate() {
  },
  // 挂载之前
  beforeMount() {
  },
  // 更新之前
  beforeUpdate() {
  },
  // 更新之后
  updated() {
  },
  // 销毁之前
  beforeDestroy() {
  },
  // 销毁之后
  destroyed() {
  },
  // 如果页面有keep-alive缓存功能,这个函数会触发
  activated() {
  },
}
</script>

<style scoped>

</style>
